package com.shun.auth.service;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.shun.auth.dto.UserLoginDto;
import com.shun.base.api.ShunBaseApi;
import com.shun.base.dto.SysLoginLogAddDto;
import com.shun.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class AuthService {

    @Autowired
    private ShunBaseApi baseApi;

    @Async
    public void addLoginLog(UserLoginDto loginDto, String acc,R result, String userAgentStr,String clientIp){
        log.debug("用户登录日志,记录：{}",acc);
        SysLoginLogAddDto logDto = new SysLoginLogAddDto();
        logDto.setLoginType(loginDto.getCheckType());
        UserAgent userAgent = UserAgentUtil.parse(userAgentStr);
        logDto.setBrowser(userAgent.getBrowser().toString());
        logDto.setOs(userAgent.getOs().toString());
        logDto.setStatus(result.ifSuc()?"0":"1");
        logDto.setIpaddr(clientIp);
        logDto.setUserName(acc);
        logDto.setMsg(result.getMsg());
        logDto.setLoginTime(LocalDateTime.now());
        R addResult = baseApi.addSysLoginLog(logDto);
        log.debug("新增日志结果：{}",addResult);
    }
}
